package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "文件上传FastDFS")
@RestController
@RequestMapping("admin/product")
public class FiledController {
    @Value("${fileServer.url}")
    private String fileUrl;
    @ApiOperation(value = "图片上传")


    @RequestMapping("fileUpload")
    public Result fileUpload(MultipartFile file) throws IOException, MyException {
        //全路径
        String url = ClassUtils.getDefaultClassLoader()
                .getResource("fdfs_client.conf").getPath();
        ClientGlobal.init(url);
        //192.168.200.128 链接tracker服务器

        TrackerClient trackerClient = new TrackerClient();

        //stoger的 IP post
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建storage的客户端，目的是为了链接storage服务器
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,null);

        String ext = FilenameUtils.getExtension(file.getOriginalFilename());

        String path = storageClient1.upload_file1(file.getBytes(), ext, null);

        String pathPlus = fileUrl + path;
        System.out.println("pathPlus = -----" + pathPlus);
        return Result.ok(pathPlus);
    }
}
